package com.ruoyi.notice.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.notice.domain.SysNotices;
import com.ruoyi.notice.domain.SysRegister;
import com.ruoyi.notice.service.SysNoticesService;
import com.ruoyi.notice.service.SysRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author YL
 * @version 1.0
 * @date 2020/7/6 13:49
 */
@RestController
@RequestMapping("register")
public class SysRegisterController extends BaseController {

    @Autowired
    private SysRegisterService sysRegisterService;
    @Autowired
    private SysNoticesService sysNoticesService;
    /**
     * 查询${tableComment}
     */
    @GetMapping("get/{noticeId}")
    public List<SysRegister> get(@PathVariable("noticeId") Long noticeId)
    {
        return sysRegisterService.selectSysRegisterById(noticeId);

    }

    /**
     * 查询【请填写功能名称】列表
     */
    @GetMapping("list")
    public R list(SysRegister sysRegister)
    {
        return result(sysRegisterService.selectMaterial(sysRegister));
    }


    /**
     * 新增保存【请填写功能名称】
     */
    @PostMapping("save")
    public R addSave(@RequestBody SysRegister sysRegister) {
        SysNotices sysNotices=sysNoticesService.selectNoticeById(12L);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        try {
            Date time=df.parse(sysRegister.getTimes());
            Date deadline=df.parse(sysNotices.getDeadline());
            int compare=time.compareTo(deadline);
            if (compare > 0){
                return R.error(400,"已超过报名截止日期！");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return toAjax(sysRegisterService.insertSysRegister(sysRegister));
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @PostMapping("update")
    public R editSave(@RequestBody SysRegister sysRegister)
    {
        return toAjax(sysRegisterService.updateSysRegister(sysRegister));
    }

}